/**
 * Created With IntelliJ IDEA
 * Description:牛客网：BM81 买卖股票的最好时机(二)
 * <a href="https://www.nowcoder.com/practice/9e5e3c2603064829b0a0bbfca10594e9?tpId=295&tqId=1073471&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj">...</a>
 * User: DELL
 * Data: 2022-11-14
 * Time: 23:03
 */

import java.util.*;

public class Solution {
    public int maxProfit (int[] prices) {
        //当prices的长度小于2时，将无法做到买入必须在卖出的前面的某一天，因此利润为0
        if (prices.length < 2) {
            return 0;
        }
        int max = 0;   //记录已遍历部分的最大利润
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                max += prices[i] - prices[i - 1];
            }
        }
        return max;
    }
}